/*
day113
2019.12.19
*/
#include<iostream>
using namespace std;

class Solution {
public:
    int arrangeCoins(int n) 
	{
        int i=0;
        while(n>=i)
        {
        	n-=i;
        	i++;
		}
		return i-1;
    }
    
    int arrangeCoins2(int n) 
    {
    	int l=0,r=n/2;
    	double n_n=1.0*n;
    	int mid;
    	while(l<r)
    	{
    		mid=l+(r-l)/2; 
    		
    		double tem=1.0*mid*(mid+1)/2;
    		
    		if(tem==n_n) return mid;
    		else if(tem>n_n)
    		{
    			r=mid-1;
			}
			else
			{
				l=mid+1;
			}
		}
		double tem=1.0*mid*(mid+1)/2;
		cout<<tem<<endl;
		return mid;
	} 
};

int main()
{
	Solution s;
	cout<<s.arrangeCoins2(5)<<endl;
	return 0;
}
